package com.hngy.xpq.android_practice.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hngy.xpq.android_practice.bean.Orders;
import com.hngy.xpq.android_practice.utils.CommOrder;
import com.hngy.xpq.android_practice.utils.Order;

import java.util.ArrayList;
import java.util.List;

public class SQLiteOrder extends SQLiteOpenHelper {       //<订单>
    SQLiteDatabase sqLiteDatabase;
    public SQLiteOrder( Context context) {
        super(context, Order.DATABASE_DATA, null, Order.DATABASE_INS);
        sqLiteDatabase = this.getReadableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + Order.DATABASE_TABLE + "("
                + Order.NOTEPAD_ID + " integer primary key autoincrement,"
                + Order.NOTEPAD_IMAGE + " BLOB,"
                + Order.NOTEPAD_USER_NAME + " VARCHAR(50),"
                + Order.NOTEPAD_PHONE + " VARCHAR(50),"
                + Order.NOTEPAD_MERCHANT_NAME + " VARCHAR(20),"
                + Order.NOTEPAD_COMM_NAME + " VACHAR(30),"
                + Order.NOTEPAD_PRICE + " VARCHAR(20),"
                + Order.NOTEPAD_TIME + " VARCHAR(50),"
                + Order.NOTEPAD_DES + " VACHAR(200),"
                + Order.NOTEPAD_STATUS + " VARCHAR(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public boolean order_add(Orders o){
        ContentValues values = new ContentValues();
        values.put(Order.NOTEPAD_USER_NAME,o.getUser_name());
        values.put(Order.NOTEPAD_IMAGE,o.getImage());
        values.put(Order.NOTEPAD_PHONE,o.getPhone());
        values.put(Order.NOTEPAD_MERCHANT_NAME,o.getMerchant_name());
        values.put(Order.NOTEPAD_COMM_NAME,o.getComm_name());
        values.put(Order.NOTEPAD_PRICE,o.getComm_price());
        values.put(Order.NOTEPAD_TIME,Order.getTimeDate());
        values.put(Order.NOTEPAD_DES,o.getDes());
        values.put(Order.NOTEPAD_STATUS,o.getStatus());
        return
                sqLiteDatabase.insert(Order.DATABASE_TABLE,null,values)>0;
    }

    public List<Orders> OrdersQuery(String merchant_name){
        List<Orders> list = new ArrayList<Orders>();
        try{
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + Order.DATABASE_TABLE + " where " + Order.NOTEPAD_MERCHANT_NAME + " = ?", new String[]{ merchant_name }, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    Orders o = new Orders();
                    o.setId(cursor.getInt(cursor.getColumnIndex(Order.NOTEPAD_ID)));
                    o.setImage(cursor.getBlob(cursor.getColumnIndex(Order.NOTEPAD_IMAGE)));
                    o.setUser_name(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_USER_NAME)));
                    o.setPhone(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_PHONE)));
                    o.setMerchant_name(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_MERCHANT_NAME)));
                    o.setComm_name(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_COMM_NAME)));
                    o.setComm_price(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_PRICE)));
                    o.setTime(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_TIME)));
                    o.setDes(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_DES)));
                    o.setStatus(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_STATUS)));
                    list.add(o);
                }
                cursor.close();
            } else {
                System.out.println("订单信息出现错误");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    public List<Orders> OrdersUserQuery(String user_name){
        List<Orders> list = new ArrayList<Orders>();
        try{
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + Order.DATABASE_TABLE + " where " + Order.NOTEPAD_USER_NAME + " = ?", new String[]{ user_name }, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    Orders o = new Orders();
                    o.setId(cursor.getInt(cursor.getColumnIndex(Order.NOTEPAD_ID)));
                    o.setImage(cursor.getBlob(cursor.getColumnIndex(Order.NOTEPAD_IMAGE)));
                    o.setUser_name(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_USER_NAME)));
                    o.setPhone(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_PHONE)));
                    o.setMerchant_name(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_MERCHANT_NAME)));
                    o.setComm_name(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_COMM_NAME)));
                    o.setComm_price(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_PRICE)));
                    o.setTime(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_TIME)));
                    o.setDes(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_DES)));
                    o.setStatus(cursor.getString(cursor.getColumnIndex(Order.NOTEPAD_STATUS)));
                    list.add(o);
                }
                cursor.close();
            } else {
                System.out.println("订单信息出现错误");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    public boolean UpdateOrder(int id,String status){
        String sql = Order.NOTEPAD_ID + " =? ";
        String[] string = new String[]{String.valueOf(id)};
        ContentValues values = new ContentValues();
        values.put(Order.NOTEPAD_STATUS,status);
        return
                sqLiteDatabase.update(Order.DATABASE_TABLE,values,sql,string)>0;
    }
}
